<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Salary_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	function get_all_salary()
	{
		$this->db->select('*');
		$this->db->from('salary_account');
		$this->db->join('users','salary_account.user_id = users.id');
	return $this->db->get()->result();	
	}
	
	function check_duplicate_entry($data)
	{
		return $this->dd->get_where('salary_account',$data)->num_rows();		
		/*if($query > 0){
		return "Duplicate Data";
		}
		else {
		return FALSE;
		}*/
	}
	
	function create_salary($insert)
	{
		$this->db->insert('salary_account',$insert);		
	}
	
	public function get_salary($id){
		return $this->db->get_where('salary_account',array('user_id'=>$id))->row_array();
	}
	
	public function update_salary($update,$id)
	{
		//print_r($update);
		$this->db->update('salary_account',$update,array('user_id'=>$id));
	}
	
	/**
	* @package : generate salary slip
	* @author: pratik
	* Salary
	* 
*/

	public function get_salary_slip($id,$month,$year){
		$where = array(
		'year_id' => $year,
		'month_id'=> $month,
		'user_id'=> $id,
		);
		$this->db->select('*');
		$this->db->from('salary_details');
		$this->db->where($where);
		return $this->db->get()->row_array();
	}
	public function generate_salary_slip(){
		
	}
	
	public function get_holidays($month_id,$year_id){		
		$query = $this->db->get_where('holidays',array('month_id'=>$month_id,'year_id'=>$year_id));
		return $query->num_rows();
	}
	
	public function days_absent($where){
		$absent_days = 0;
		$absents = $this->db->get_where('daily_attendance_sheet',$where)->result_array();
		if($absents){
		foreach($absents as $key=>$value){
			foreach($value as $val){
				if($val=='absent' OR $val=='rejected'){
				$absent_days +=1;
				}
			}			
		}		
		return $absent_days;
		}else{
			return 0;
		}		
	}
	
	public function present($where){
		$present_days = array();		
		/*getting the attendance of present*/
		
		$this->db->select();
		$this->db->from('daily_attendance_sheet');
		$this->db->where($where);
		$presents = $this->db->get()->result_array();
		
		//print_r($presents);
		unset($presents[0]['id']);
		unset($presents[0]['year_id']);
		unset($presents[0]['month_id']);
		unset($presents[0]['user_id']);		
		
			/*getting the loop of present*/
			
			if($presents){
				foreach($presents as $key=>$value){
					foreach($value as $k=>$val){						
						if(!($val=='absent') AND !($val=='sunday') AND !($val=='holiday') AND !($val=='paid_leave') AND !($val=='rejected') AND !($val=='unpaid_leave')){
						echo $k;
						echo '<pre>';
						}
					}			
				}				
			}else{
				return 0;
				}	
	}
	
	public function unpaid_leave($where){
		$unpaid_leave = 0;
		$absents = $this->db->get_where('daily_attendance_sheet',$where)->result_array();
		if($absents){		
		foreach($absents as $key=>$value){
			foreach($value as $val){
				if($val=='unpaid_leave'){
				$unpaid_leave +=1;
				}
			}			
		}		
		return $unpaid_leave;
		}else{
			return 0;
		}
	}
	
	public function paid_leave($where){
		$paid_leave = 0;
		$absents = $this->db->get_where('daily_attendance_sheet',$where)->result_array();
		if($absents){		
		foreach($absents as $key=>$value){
			foreach($value as $val){
				if($val=='paid_leave'){
				$paid_leave +=1;
				}
			}			
		}		
		return $paid_leave;
		}else{
			return 0;
		}	
	}
	
	public function half_day($where){
		$half_day = 0;
		$absents = $this->db->get_where('daily_attendance_sheet',$where)->result_array();		
		
		unset($absents[0]['id']);
		unset($absents[0]['year_id']);
		unset($absents[0]['month_id']);
		unset($absents[0]['user_id']);
		
		if($absents){
		foreach($absents as $key=>$value){
			
			foreach($value as $val){
				if(($val != 'absent') AND ($val != 'paid_leave') AND ($val != 'unpaid_leave')  AND ($val != 'holiday') AND ($val != 'sunday') AND ($val != 'rejected') AND ($val!="")){
				
				$time = explode(':',$val);
				if($time[0] < 4){
				$half_day +=1;
					}
				}
			}
		}
		return $half_day;
	}else{
			return 0;
		}
	}
	
	public function add_payslip($insert){
		
		// check salary updated or not 
		
		if($salary_id){		
		}else{
			$this->db->insert('salary_details',$insert);
		}		
	}
	
	public function update_payslip_data($update){
		$where = array(
			'id' => $update['id'],			
		);
		$salary_id = $this->db->get_where('salary_details',$where)->row_array();
		unset($update['id']);		
		$this->db->update('salary_details',$update,array('id'=>$salary_id['id']));
		
	}
	
	public function per_day_salary($id){		
		return $this->db->get_where('salary_account',array('user_id'=>$id))->row_array();
	}	
	
	function delete($where){
		$this->db->delete('salary_details',$where);
	}
	
	public function fine($where){		
		$late = 0;
		$days_fine = NULL;
		$this->db->select('entry_time');
		$this->db->from('attendance_timings');
		$this->db->where($where);
		$present = $this->db->get()->result_array();
		
		foreach($present as $key=>$value){
			
			foreach($value as $val){
				$entry=strtotime($val);
				$to_be_time=strtotime('10:00:00');
				
				if($entry>$to_be_time){
					$late +=1;
					$fine = (float)($late*50);					
					$days_fine=array('days'=>$late,'amount'=>$fine,'fine_charges'=>"50");
				}
			}
		}
		//if($days_fine){
		return $days_fine;	
		/*}else{
			return $days_fine = array('days'=>0,'amount'=>0,'fine_charges'=>"50");
		}*/
		
	}
	
}
